package controllers

import (
	"doc-heal/models"
	beego "github.com/beego/beego/v2/server/web"
)

type Doctor struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (d *Doctor) Add() {

	name := d.GetString("name")
	tel := d.GetString("tel")
	doctors := d.GetString("doctors")
	department := d.GetString("department")
	nickname := d.GetString("nickname")
	graphic := d.GetString("graphic")
	telephone := d.GetString("telephone")
	registered := d.GetString("registered")
	doctorsReview := d.GetString("doctorsReview")
	serviceAudits := d.GetString("serviceAudits")
	doctor := models.Doctor{
		Name:          name,
		Tel:           tel,
		Doctors:       doctors,
		Department:    department,
		Nickname:      nickname,
		Graphic:       graphic,
		Telephone:     telephone,
		Registered:    registered,
		DoctorsReview: doctorsReview,
		ServiceAudits: serviceAudits,
	}
	err := models.DB.Create(&doctor).Error
	if err != nil {
		d.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
		}
		d.ServeJSON()
		return
	} else {
		d.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
		}
		d.ServeJSON()
	}
}
func (d *Doctor) List() {

	page, _ := d.GetInt("page", 1)
	size, _ := d.GetInt("size", 5)
	offset := (page - 1) * size
	query := models.DB.Model(&models.Doctor{})
	var count int64
	query.Count(&count)
	var doctor []models.Doctor
	err := query.Offset(offset).Limit(size).Find(&doctor).Error
	if err != nil {
		d.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
		}
		d.ServeJSON()
		return
	} else {
		PageData := make(map[string]interface{})
		PageData["count"] = count
		PageData["doctor"] = doctor
		d.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: PageData,
		}
		d.ServeJSON()
	}
}
